var util = require('../../../utils/util.js');
var API = require('../../../api_new/api.js');

Page({
  data: {
    orderNumber: '',
    orderInfo: {},
    orderGoods: [],
    handleOption: {}
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      orderNumber: options.orderNumber
    });
    this.getOrderDetail();
  },
  getOrderDetail() {
    let that = this;
    API.methodPost('order', 'OrderDetail', {
      orderNumber: that.data.orderNumber
    }).then(function(res) {
      if (res.code === 0) {
        console.log(res.data);
        that.setData({
          orderInfo: res.data.orderInfo,
          orderGoods: res.data.orderGoods,
          handleOption: res.data.handleOption
        });
        //that.payTimer();
      }
    });
  },
  returnOrder() {
    util.redirect('/pages/ucenter/orderReturn/orderReturn?orderNumber=' + this.data.orderInfo.orderNumber);

  },
  payTimer() {
    let that = this;
    let orderInfo = that.data.orderInfo;

    setInterval(() => {
      console.log(orderInfo);
      orderInfo.add_time -= 1;
      that.setData({
        orderInfo: orderInfo,
      });
    }, 1000);
  },
  cancelOrder() {
    console.log('开始取消订单');
    let that = this;
    let orderInfo = that.data.orderInfo;
    console.log(orderInfo);

    var order_status = orderInfo.orderStatus;
    console.log(order_status);
    wx.showModal({
      title: '',
      content: '确定要取消此订单？',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定');
          API.methodPost('order', 'OrderCancel', {
            orderNumber: orderInfo.orderNumber
          }).then(function(res) {
            if (res.code === 0) {
              console.log(res.msg);
              wx.showModal({
                title: '提示',
                content: res.msg,
                showCancel: false,
                confirmText: '继续',
                success: function(res) {
                  util.redirect('/pages/ucenter/order/order');
                  // wx.navigateBack({
                  //   url: 'pages/ucenter/order/order',
                  // });
                }
              });
            }
          });

        }
      }
    });
  },
  payOrder() {
    let that = this;
    API.methodPost('order', 'PayPrepayId', {
      orderNumber: that.data.orderNumber
    }).then(function(res) {
      if (res.code === 0) {
        const payParam = res.data;
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.package,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function(res) {
            wx.redirectTo({
              url: '/pages/payResult/payResult?status=true&orderNumber=' + that.data.orderNumber
            })
          },
          'fail': function(res) {
            wx.redirectTo({
              url: '/pages/payResult/payResult?status=false&orderNumber=' + that.data.orderNumber
            })
          }
        });
      }
    });

  },
  confirmOrder() {
    //确认收货
    console.log('开始确认收货');
    let that = this;
    let orderInfo = that.data.orderInfo;
    console.log(orderInfo);

    var order_status = orderInfo.orderStatus;
    console.log(order_status);

    var errorMessage = '';
    switch (order_status) {
      // case 300: {
      //   console.log('已发货，不能取消');
      //   errorMessage = '订单已发货';
      //   break;
      // }
      case 301:
        {
          console.log('已收货，不能再收货');
          errorMessage = '订单已收货';
          break;
        }
      case 101:
        {
          console.log('已经取消');
          errorMessage = '订单已取消';
          break;
        }
      case 102:
        {
          console.log('已经删除');
          errorMessage = '订单已删除';
          break;
        }
      case 401:
        {
          console.log('已经退款');
          errorMessage = '订单已退款';
          break;
        }
      case 402:
        {
          console.log('已经退款退货');
          errorMessage = '订单已退货';
          break;
        }
    }

    if (errorMessage != '') {
      console.log(errorMessage);
      util.showErrorToast(errorMessage);
      return false;
    }

    console.log('可以取消订单的情况');
    wx.showModal({
      title: '',
      content: '确定已经收到商品？',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定');
          API.methodPost('order', 'OrderConfirm', {
            orderNumber: orderInfo.orderNumber
          }).then(function(res) {
            if (res.code === 0) {
              console.log(res.data);
              wx.showModal({
                title: '提示',
                content: res.data,
                showCancel: false,
                confirmText: '继续',
                success: function(res) {
                  //  util.redirect('/pages/ucenter/order/order');
                  wx.navigateBack({
                    url: 'pages/ucenter/order/order',
                  });
                }
              });
            }
          });

        }
      }
    });
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  }
})